This notebook illustrates how to use ipyaudio
for interactive and realtime DSP.
In [1]:
# First, load up our ipyaudio module.
import ipyaudio
In [2]:
# Our example function will simply compute the root-mean-squared energy of the buffer
# We'll use IPython's display module to keep the output stationary on screen.
import numpy as np
import IPython.display
def rmse(y, sr):
en = np.sqrt(np.sum(y**2.0))
print 'RMSE: {:0.3e}'.format(en)
IPython.display.clear_output(wait=True)
In general, callback functions must support the following positional arguments:
y
: the audio buffer (np.ndarray)sr
: the sampling rate of the buffer
In [3]:
# Now, let's make an audio connector object
audio_connector = ipyaudio.AudioConnector(rmse, output=True, sr=22050)
# And connect it up to a toggle button
play_widget = ipyaudio.playback_widget(audio_connector)
In [4]:
# And display it in the IPython console
play_widget
In [5]:
# More advanced: keep a running average of rmse
import time
def buffered_rmse(y, sr, alpha=0.5, state=None):
en = np.sqrt(np.sum(y**2.0))
#time.sleep(0.0)
if state is None:
raise ValueError('Invalid state buffer!')
if 'en' not in state:
state['en'] = en
else:
state['en'] = alpha * state['en'] + (1-alpha) * en
print 'RMSE: {:0.3e}'.format(state['en'])
IPython.display.clear_output(wait=True)
In [6]:
# Now, let's make an audio connector object
my_state = {}
audio_connector = ipyaudio.AudioConnector(buffered_rmse, output=True, window=128, sr=22050, alpha=0.9, state=my_state)
# And connect it up to a toggle button
play_widget = ipyaudio.playback_widget(audio_connector)
In [7]:
play_widget
In [8]:
my_state
Out[8]:
In [26]:
def bpf(y, sr, low=16, high=64):
yhat = np.fft.rfft(y)
yhat[:low] = 0.0
yhat[high:] = 0.0
return np.fft.irfft(yhat)
In [27]:
# Now, let's make an audio connector object
my_state = {}
audio_connector = ipyaudio.AudioConnector(bpf, output=True, window=256, sr=22050)
# And connect it up to a toggle button
play_widget = ipyaudio.playback_widget(audio_connector)
In [28]:
play_widget